<template>
    <div>
        <Header/>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="摘要" prop="description">
                <el-input v-model="ruleForm.description"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="content">
                <mavon-editor v-model="ruleForm.content"></mavon-editor>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import Header from '@/components/Header'
export default {
    name: "BLogEidt",
    components: {Header},
    data() {
      return {
        ruleForm: {
          id: '',
          title: '',
          description: '',
          content: ''
        },
        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 2, max: 120, message: '长度在 2 到 120 个字符', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入摘要', trigger: 'change' }
          ],
          content: [
            { required: true, message: '请输入内容', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        const _this = this;
        this.$refs[formName].validate((valid)=>{
          if(valid){
            this.$axios.post("/blog/save", this.ruleForm).then(res =>{
              _this.$alert('操作成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                      _this.$router.push('/blogs')
                  }
              })
            });
          }else{
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
        const bid = this.$route.params.bid;
        if(bid) {
            const _this = this;
            this.$axios.get('/blog/'+bid).then(res => {
                const blog = res.data.data;
                _this.ruleForm.id = blog.id;
                _this.ruleForm.title = blog.title;
                _this.ruleForm.description = blog.description;
                _this.ruleForm.content = blog.content;
                //_this.ruleForm = blog;
            })
        }
    }
}
</script>
<style scoped>

</style>